#!/usr/bin/env bash

# 开发
dev_server="192.168.1.179"
# 上线
pro_server="211.149.179.219"
# 端口号
#server_port=22
 server_port=22000
# 文件路径
file_path="/usr/local/bin"
# 服务器
run_server=${dev_server}
# 密码
server_pwd="lovekaka"
#server_pwd="Lovekaka321"

echo "==================================================="
echo "             server = ${run_server}"
echo "===================开始上传文件======================"

# >>>>>>>>>>>>>>>>>>>>>>判断avs文件是否存在
#if test ! -d "../../facade/avs/target/";
#then
#    echo "文件目录不存在";exit -1;
#fi
#if test ! -f "../../facade/avs/target/avs-0.0.1.jar";
#then
#    echo "文件不存在";exit -1;
#fi
# >>>>>>>>>>>>>>>>>>>>>>上传脚本文件
/usr/bin/expect <<-EOF
send_user "====================上传脚本文件======================\n"
spawn scp -P ${server_port} runfile.sh root@${run_server}:${file_path}
expect {
   "*yes/no" {send "yes\r";exp_continue}
   "*password:" {send "${server_pwd}\r"}
}
expect "*#"
EOF
# 上传文件至服务器目录

# >>>>>>>>>>>>>>>>>>>>>>上传avs文件
#cd ../../facade/avs/target/
#/usr/bin/expect <<-EOF
#send_user "====================上传jar文件======================\n"
#spawn scp -P ${server_port} avs-0.0.1.jar root@${run_server}:${file_path}
#expect {
#   "*yes/no" { send "yes\r";exp_continue }
#   "*password:" { send "${server_pwd}\r" }
#}
#expect "*#"
#EOF

# >>>>>>>>>>>>>>>>>>>>>>判断platfrom文件是否存在
# if test ! -d "../../facade/platfrom/target/";
# then
#     echo "文件目录不存在";exit -1;
# fi
# if test ! -f "../../facade/platfrom/target/platfrom-0.0.1.jar";
# then
#     echo "文件不存在";exit -1;
# fi

# >>>>>>>>>>>>>>>>>>>>>>上传platfrom文件
#cd ../../facade/platfrom/target/
##cd ../../platfrom/target/
#/usr/bin/expect <<-EOF
#send_user "====================上传jar文件======================\n"
#spawn scp -P ${server_port} platfrom-0.0.1.jar root@${run_server}:${file_path}
#expect {
#   "*yes/no" { send "yes\r";exp_continue }
#   "*password:" { send "${server_pwd}\r" }
#}
#expect "*#"
#EOF

/usr/bin/expect <<-EOF
send_user "\n====================登陆服务器======================\n"
## 登陆服务器 执行命令
spawn ssh root@${run_server} -p ${server_port}
expect {

   "*yes/no" { send "yes\r";exp_continue }
   "*password:" { send "${server_pwd}\r" }
}
expect "*#"
send "cd ${file_path}\r"
expect "*#"
send "chmod +x runfile.sh\r"
expect "*#"
# >>>>>>>>>>>>>>>>>>>>>>运行avs文件
send "./runfile.sh avs-0.0.1 8088\r"
#expect "*#"
# >>>>>>>>>>>>>>>>>>>>>>运行platfrom文件
# send "./runfile.sh platfrom-0.0.1 8080\r"
 expect "*==================================================="
send "exit -1 \r"
expect eof
EOF

# cd ../../facade/platfrom/target/
# scp avs-0.0.1.jar runfile.sh root@192.168.1.179:/usr/local/bin
# ssh root@192.168.1.179 'cd /usr/local/bin;chmod +x avs.sh;./runfile.sh platfrom-0.0.1.jar 8080'
